package com.kofigyan.stateprogressbar.utils;

import ohos.agp.components.AttrSet;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import ohos.app.Context;

public class AttrSetUtils {

    public static int optInt(AttrSet attrSet,
                              String key,
                              int def) {
        if (attrSet == null) {
            return def;
        }
        return attrSet.getAttr(key).isPresent()
                ? attrSet.getAttr(key).get().getIntegerValue() : def;
    }

    public static String optString(AttrSet attrSet,
                                   String key,
                                   String def) {
        if (attrSet == null) {
            return def;
        }
        return attrSet.getAttr(key).isPresent()
                ? attrSet.getAttr(key).get().getStringValue() : def;
    }

    public static float optPixelSize(AttrSet attrSet,
                                      String key,
                                      float def) {
        if (attrSet == null) {
            return def;
        }

        return attrSet.getAttr(key).isPresent()
                ? attrSet.getAttr(key).get().getDimensionValue() : def;
    }

    public static int optPixelSize(AttrSet attrSet,
                                    String key,
                                    int def) {
        if (attrSet == null) {
            return def;
        }

        return attrSet.getAttr(key).isPresent()
                ? attrSet.getAttr(key).get().getDimensionValue() : def;
    }

    public static int optColor(AttrSet attrSet,
                                String key,
                                int def) {
        if (attrSet == null) {
            return def;
        }
        return attrSet.getAttr(key).isPresent()
                ? attrSet.getAttr(key).get().getColorValue().getValue() : def;
    }

    public static boolean optBoolean(AttrSet attrSet,
                                      String key,
                                      boolean def) {
        if (attrSet == null) {
            return def;
        }
        return attrSet.getAttr(key).isPresent()
                ? attrSet.getAttr(key).get().getBoolValue() : def;
    }

    public static Element optElement(AttrSet attrSet,
                                     String key,
                                     Element def) {
        if (attrSet == null) {
            return def;
        }
        return attrSet.getAttr(key).isPresent()
                ? attrSet.getAttr(key).get().getElement() : def;
    }

    public static Element optElement(Context context,
                                     AttrSet attrSet,
                                     String key,
                                     int def) {

        PixelMapElement defElement = new PixelMapElement(
                PixelMapUtils.getPixelMapFromResource(context,
                        def)
        );

        if (attrSet == null) {
            return defElement;
        }
        return attrSet.getAttr(key).isPresent()
                ? attrSet.getAttr(key).get().getElement() : defElement;
    }

}
